﻿using UnityEngine;
using System.Collections;

public class RaptorController : MonoBehaviour {
	private int moveX;
	private int moveY;
	private int tieBreaker;
	private GameObject planet;
	public float speed = 8f;
	private Transform enemyShip;
	public Transform playerShip;
	public AudioClip explosionSound;
	public int impactDamage = 50;
	public int raptorHealth = 2;

	private GameObject pRef;

	// holds the distance between the ship and the player
	private float distance;
	
	// holds the rotation adjustment for the ship to face the player
	private Vector3 targetRot;
	private Quaternion tempRot;
	
	// the vector that the ship partially rotates around to attack the player
	private Vector3 attackVector;
	
	// total degrees in which the ship has rotated around the attack vector
	private int tDeg;
	
	// holds the direction the ship was headed in toward the player
	// before entering attacking state
	private Vector3 direction;
	
	public float fireRate;
	private float nextFire;
	
	public Transform weaponShotPosition;
	public GameObject weaponShot;
	public GameObject planetShot;

	private bool attacked = false;
	private bool dead = false;

	public float attackStateTime = 5.0f;
	public float followStateTime = 5.0f;
	
	// ship AI states
	public enum States
	{
		asleep = 0,
		following = 1,
		attacking = 2,
		attackingPlr = 3
	}
	
	// define States as a var and set default to asleep
	public States currentState = States.asleep;
	
	// Update is called once per frame
	void Start(){
		//set the transform data to enemyShip var
		enemyShip = transform;
		planet = GameObject.FindGameObjectWithTag("Planet");
		if (Random.Range(1, 10) < 5)
			currentState = States.attacking;
		else
			currentState = States.following;
	}
	
	void Update () {

		if (!dead)
		{
		/*//Initiate movement direction
		transform.Translate (new Vector3 (moveX, 0, moveY) * speed * Time.deltaTime);
		*/
			// get our player references
		pRef = GameObject.FindGameObjectWithTag("Player");
		
		switch (currentState)
		{
			// do nothing if the ship is asleep
		case States.asleep:
			break;
		case States.following:
			if (Time.time > followStateTime)
			{
					attackStateTime = Time.time + attackStateTime;
					currentState = States.attacking;
					break;
			}

			// calculate the distance between the ship and the player
			distance = Vector3.Distance(this.transform.position, pRef.transform.position); 
			
			if (attacked)
			{
				if (tDeg < 45)
				{
					// increment the total degrees
					tDeg += 1;
					this.transform.RotateAround(pRef.transform.position, Vector3.back, (speed * 8) * Time.deltaTime / (distance / 12));
				}
				else
				{
					attacked = false;
					tDeg = 0;
				}
			}
			// if the ship is not close enough, move toward the player
			else if (distance > 5f)
				transform.position = Vector3.MoveTowards (this.transform.position, pRef.transform.position, speed * Time.deltaTime);

			if (Time.time > nextFire)
			{
				nextFire = Time.time + fireRate;	
				Instantiate(weaponShot, weaponShotPosition.position, enemyShip.rotation);
				
			}

			break;
		case States.attacking:
			if (Time.time > attackStateTime)
			{
				followStateTime = Time.time + followStateTime;
				currentState = States.following;
				break;
			}
		
			direction = (planet.transform.position - this.transform.position);
			distance = Vector3.Distance(this.transform.position, planet.transform.position);

			direction.z = 0;
			direction *= 20;

			if (!attacked)
			{
				tDeg = 0;

				if (distance < 20)
					this.transform.position = Vector3.MoveTowards(this.transform.position, -direction, Time.deltaTime * speed);
				else if (distance > 25)
					this.transform.position = Vector3.MoveTowards(this.transform.position, direction, Time.deltaTime * speed);
			}
			else
			{
					if (tDeg < 45)
					{
						// increment the total degrees
						tDeg += 1;
						this.transform.RotateAround(pRef.transform.position, Vector3.back, (speed * 8) * Time.deltaTime / (distance / 12));
					}
			}

			if (Time.time > nextFire && !attacked)
			{
				nextFire = Time.time + fireRate;	
				Instantiate(planetShot, weaponShotPosition.position, enemyShip.rotation);			
			}

			if (attacked)
			{

				if (Vector3.Angle(this.transform.forward, pRef.transform.position - this.transform.position) < 5 && Time.time > nextFire)
				{
					nextFire = Time.time + fireRate;
					Instantiate(weaponShot, weaponShotPosition.position, enemyShip.rotation);
					attacked = false;
				}
			}
			break;
		}
		
		if (currentState == States.following || attacked)
		{
			// get the rotation vector between the ship and the player
			targetRot = new Vector3(pRef.transform.position.x, pRef.transform.position.y, this.transform.position.z);

			// adjust the x and y values of the rotation to make the rotation smooth
			targetRot.x = Mathf.Lerp(transform.position.x, targetRot.x, speed * Time.deltaTime);
			targetRot.y = Mathf.Lerp(transform.position.y, targetRot.y, speed * Time.deltaTime);

			// make the ship look at the adjusted vector
			this.transform.LookAt(targetRot, Vector3.back);
		}
		else
			this.transform.LookAt(new Vector3(planet.transform.position.x, planet.transform.position.y, this.transform.position.z), Vector3.back);

		///Wrap enemy ship around screen on exit
			if(enemyShip.position.x >= 38)
			{
				enemyShip.position = new Vector3(-38, enemyShip.position.y, enemyShip.position.z);
			}
			else if(enemyShip.position.x <= -38)
			{
				enemyShip.position = new Vector3(38, enemyShip.position.y, enemyShip.position.z);
			}
			else if(enemyShip.position.y >= 20)
			{
				enemyShip.position = new Vector3(enemyShip.position.x,-20, enemyShip.position.z);
			}
			else if(enemyShip.position.y <= -20)
			{
				enemyShip.position = new Vector3(enemyShip.position.x,20, enemyShip.position.z);
			}
		}
	}
	
	void OnTriggerEnter(Collider other){
		if (other.tag == "Player") {
			GameManager.playerHealth-=impactDamage;//Cause damage to the player on impact

			raptorHealth--;

			if (!attacked)
			{
				nextFire = Time.time + 1;
				attacked = true;
			}

			if (raptorHealth <= 0)
			{
				audio.PlayOneShot(explosionSound);
				dead = true;
				renderer.enabled = false;
				collider.enabled = false;
				Destroy(this.gameObject, explosionSound.length);
			}
		}
		if (other.tag == "Bullet") {


			raptorHealth--;

			if (!attacked)
			{
				nextFire = Time.time + 1;
				attacked = true;
			}

			Destroy(other.gameObject,0);

			if (raptorHealth <= 0)
			{
				audio.PlayOneShot(explosionSound);
				dead = true;
				renderer.enabled = false;
				collider.enabled = false;
				Destroy(this.gameObject,explosionSound.length);
			}
		}
	}
}
